package com.atguigu.dga.meta.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.meta.bean.TableMetaInfo;
import com.atguigu.dga.meta.bean.TableMetaInfoExtra;
import com.atguigu.dga.meta.bean.TableMetaInfoForQuery;
import com.atguigu.dga.meta.bean.TableMetaInfoVO;
import com.atguigu.dga.meta.service.TableMetaInfoExtraService;
import com.atguigu.dga.meta.service.TableMetaInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 元数据表 前端控制器
 * </p>
 *
 * @author wuzhida
 * @since 2024-06-24
 */
@RestController
@RequestMapping("/tableMetaInfo")
public class TableMetaInfoController {

    private static final String SUCCESS = "success";

    @Autowired
    private TableMetaInfoService tableMetaInfoService;
    @Autowired
    private TableMetaInfoExtraService tableMetaInfoExtraService;


    /**
     * 元数据管理平台--查询接口
     * 请求路径：http://dga.gmall.com/tableMetaInfo/table-list?schemaName=&tableName=&dwLevel=&pageSize=20&pageNo=1
     * 
     * 请求参数：
     *        schemaName 条件
     *        tableName  条件
     *        dwLevel    条件
     *        pageSize
     *        pageNo
     *        
     * 返回参数：{
     * "total": 79,
     * "list": [
     * { "id": 1186,
     * "tableName": "ads_traffic_stats_by_channel",
     * "schemaName": "gmall",
     * "tableSize": 2254,
     * "tableTotalSize": 6762,
     * "tableComment": "各渠道流量统计",
     * "tecOwnerUserName": "weiyunhui",
     * "busiOwnerUserName": "weiyunhui",
     * "tableLastModifyTime": "2023-02-07T09:52:58.000+00:00",
     * "tableLastAccessTime": "2023-02-07T09:52:58.000+00:00"
     * },
     * { "id": 1187,
     * ….
     * }
     * ]
     * }
     * <p>
     */
    @GetMapping("/table-list")
    public String getTableMetaList(TableMetaInfoForQuery tableMetaInfoForQuery) {

        //1. 获取list集合
        List<TableMetaInfoVO> list = tableMetaInfoService.getTableMetaInfoByConditionAndPage(tableMetaInfoForQuery);

        //2. 获取total
        Integer count = tableMetaInfoService.getTableMetaInfoCount(tableMetaInfoForQuery);

        //3. 整合成json
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total", count);
        jsonObject.put("list", list);

        //4. 返回结果        
        return jsonObject.toJSONString();
    }


    /**
     * 根据id查询具体某个表的信息和辅助表信息
     * 请求路径：http://dga.gmall.com/tableMetaInfo/table/163
     * @param tableId id
     * @return
     */
    @GetMapping("/table/{tableMetaInfoId}")
    public TableMetaInfo table(@PathVariable("tableMetaInfoId") Long tableId) {
        TableMetaInfo tableMetaInfo = tableMetaInfoService.getById(tableId);

        //辅助表信息
        TableMetaInfoExtra metaInfoExtra = tableMetaInfoExtraService.getOne(
                new QueryWrapper<TableMetaInfoExtra>()
                        .eq("schema_name", tableMetaInfo.getSchemaName())
                        .eq("table_name", tableMetaInfo.getTableName())
        );
        
        tableMetaInfo.setTableMetaInfoExtra(metaInfoExtra);

        return tableMetaInfo;
    }

    /**
     * 保存辅助信息
     * 请求路径：http://dga.gmall.com/tableMetaInfo/tableExtra
     * @param tableMetaInfoExtra
     * @return
     */
    @PostMapping("/tableExtra")
    public String tableExtra(@RequestBody TableMetaInfoExtra tableMetaInfoExtra){
        tableMetaInfoExtra.setUpdateTime(new Date());
        tableMetaInfoExtraService.updateById(tableMetaInfoExtra);
        return SUCCESS;
    }

    /**
     * 手动更新全库元数据
     * 请求路径：http://dga.gmall.com/tableMetaInfo/init-tables/gmall/2024-06-11
     * @return
     */
    @PostMapping("/init-tables/{schemaName}/{assessDate}")
    public String initTables(@PathVariable("schemaName") String schemaName, @PathVariable("assessDate") String assessDate){
        try {
            tableMetaInfoService.initTableMetaInfo(schemaName, assessDate);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return SUCCESS;
    }
}
